//
//  WZNetWorkStatusManager.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/19.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "WZNetWorkStatusManager.h"

@implementation WZNetWorkStatusManager

+(void)show:(UIView *)view type:(WZNetWorkStatusType)type msg:(NSString *)msg subMsg:(NSString *)subMsg offsetY:(CGFloat)offsetY failClickBlock:(failBlock)failClickBlock{
    
    //先移除一次
    [self dismiss:view];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        //创建CMView
        WZNetWorkStatusView *myCmView=[WZNetWorkStatusView ViewWithType:type msg:msg subMsg:subMsg offsetY:offsetY failClickBlock:failClickBlock];
        
        [view addSubview:myCmView];
        
        myCmView.alpha=0;
        [UIView animateWithDuration:.25f animations:^{
            myCmView.alpha=1.0f;
        }];
    });
    
}



+(void)dismiss:(UIView *)view{
    
    NSArray *subViews=view.subviews;
    
    if(subViews==nil || subViews.count==0) return;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //遍历
        for (UIView *subView in subViews) {
            
            if(![subView isKindOfClass:[WZNetWorkStatusView class]]) continue;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [UIView animateWithDuration:0.5f animations:^{
                    subView.alpha=0;
                } completion:^(BOOL finished) {
                    if (finished) {
                        [subView removeFromSuperview];
                    }
                }];
                
            });
        }
    });
}


@end
